<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <!--{include file='comm/header.tpl'}-->
    <title>商品营养成分信息列表</title>
    <!--{include file="comm/icheck.tpl"}-->
</head>

<body>
<div class="container">

    <div class="comm_content">

            <div class="search-box">
                <div style="position:relative">
                    <form id="search_form" onsubmit="return false;" class="form-inline">
                    	<span>
                        	营养成分信息组名称：<input type="text" name="nGroupName" id="nGroupName1" class="form-control w150" />
                        </span>

                        	使用状态：
                            <select  class="form-control w150" id="useFlg" name="useFlg">
                                <option value="">请选择</option>
                                <option value="1">启用</option>
                                <option value="0">禁用</option>
                            </select>
                        </span>
                        <span>
                        	&nbsp;&nbsp;&nbsp;<input type="button" value="查询" class="btn btn-primary" onclick="goodsNutrition.search();" />
                        </span>
                        <span>
                            <button onclick="goodsNutrition.clearUp();" id="clearBtn" class="btn btn-warning" type="button">重置</button>
                        </span>
                    </form>
                </div>
            </div>

            <table id="flex" style="display:none"></table>

        </div>

</div>

<div id="pop" style="display: none;">

    <form class="form-horizontal" id="editForm" onsubmit="return false;">

        <div class="form-group">
            <label for="french" class="col-sm-3 control-label"><span class="red">*</span> 营养成分信息组名称：</label>
            <div class="col-sm-9">
                <input type="text" class="form-control w300" id="nGroupName" name="nGroupName" />
            </div>
        </div>

        <div class="form-group">
            <label for="sortOrder" class="col-sm-3 control-label"><span class="red">*</span> 排序：</label>
            <div class="col-sm-9">
                <input type="text" class="form-control w150" id="sortOrder" name="sortOrder" />
            </div>
        </div>

        <button class="btn btn-default" onclick="goodsNutrition.specAdd();" type="button">添加基本信息</button>
        <div class="specTable" style="width:694px;">
            <table class="table table-bordered table-hover">
                <thead>
                <tr>
                    <th width="30%" style="text-align:center;">营养成分值</th>
                    <th width="30%" style="text-align:center;">营养成分百分比</th>
                    <th width="20%" style="text-align:center;">使用标志</th>
                    <th width="20%" style="text-align:center;">操作</th>
                </tr>
                </thead>

                <tbody class="specBody"></tbody>
            </table>
        </div>
        <input type="hidden" name="nGroupID" id="nGroupID" value="" />

    </form>

</div>

<script type="text/javascript">

$("#flex").flexigrid({
    url:"/invokingAjax-backend/goods/goodsNutrition.ajax?operFlg=1",
    dataType: "json",
    colModel : [
        <!--{if isset($edit_permission)}-->
        {display: "操作", width : 100, align: "center"},
        <!--{/if}-->
        {display: "序号", name : "sortOrder",sortable : true, width : 100},
        {display: "营养成分信息组名称", width : 230 },
        {display: "营养成分值",width : 200, },
        {display: "营养成分百分比",width : 200},
        {display: "使用状态", width : 100 , align: "center"}
    ],
    <!--{if isset($edit_permission)}-->
    buttons : [
        {name: "添加", bclass: "add", width : 200, onpress : "goodsNutrition.edit"},
        //{separator: true},
        //{name: "删除", bclass: "delete", onpress : "goodsNutrition.del.check"}
    ],
    <!--{/if}-->
    sortname: "sortOrder",
    sortorder: "asc",
    usepager: true,
    rp: 10,
    useRp: true,
    showTableToggleBtn: true,
    onSubmit: function() {
        var dt = $('#search_form').serializeArray();
        $("#flex").flexOptions({params: dt});
        return true;
    }
});

var goodsNutrition = {

    i : 0,  //添加基本信息
    useFlg_str : "" ,
    dt:"",
    //搜索规格
    search: function() {
        var dt = $('#search_form').serializeArray();
        $("#flex").flexOptions({newp: 1,params: dt}).flexReload();
    },

    doAction: function( nGroupID , useFlg ) {
        this.nGroupID = nGroupID;
        this.useFlg = useFlg;
        if( useFlg == 0 ) {
            E.confirm("您确认要禁用此商品营养成分信息吗？", "goodsNutrition.actionConfirm");
        } else {
            E.confirm("您确认要启用此商品营养成分信息吗？", "goodsNutrition.actionConfirm");
        }

    },

    actionConfirm : function () {

        E.loadding.open('请稍候...');
        $.ajax({
            type: "POST",
            url: "/invokingAjax-backend/goods/goodsNutrition.ajax?operFlg=6",
            dataType: "JSON",
            data : {
                nGroupID: this.nGroupID,
                useFlg: this.useFlg
            },
            success: function( result_obj ) {
                E.loadding.close();
                if (result_obj.code == 200) {
                    E.alert(result_obj.message, 2, "goodsNutrition.load");
                } else {
                    E.alert(result_obj.message);
                }
            }
        });
    },

    //编辑或添加弹出框
    edit: function(nGroupID) {

        E.loadding.open('请稍候...');
        var name="";

        if(nGroupID){
            name="修改商品商品营养信息";
            E.ajax_post({
                url: "/invokingAjax-backend/goods/goodsNutrition.ajax?operFlg=2",
                data:{
                    nGroupID : nGroupID
                },
                call:"goodsNutrition.val"
            });

        }else{
            name="添加商品营养成分信息";
            E.ajax_post({
                url:"/invokingAjax-backend/goods/goodsNutrition.ajax?operFlg=3" ,
                call:"goodsNutrition.sortUp"
            });

        }
        goodsNutrition.i=0;
        E.popup.open({
            id:"pop",
            title: name,
            css: "width: 760px;margin:-10px auto;overflow:visible;",
            btnName: "保存",
            btnCall:"goodsNutrition.check();"
        });
    },


    //编辑弹出层赋值
    val: function(o){
        E.loadding.close();
        $("#nGroupName").val(o.nGroupName);
        $("#sortOrder").val(o.sortOrder);

        //修改时需要
        $("#nGroupID").val(o.nGroupID);

        //$('.specBody').html('');

        //规格值
        var basicStr = '';

        for(var i=0; i<o.goods_basic.length; i++) {

            goodsNutrition.i++;

            basicStr += '<tr>';

            basicStr += '<td style="text-align:center;">';
            basicStr += '<input type="text" class="form-control w200" name="basic_val'+goodsNutrition.i+'" value='+o.goods_basic[i]['name']+' style="width:150px;margin:auto;" maxlength="20">';
            basicStr += '</td>';

            basicStr += '<td style="text-align:center;">';
            basicStr += '<input  class="form-control w200" type="text" name="percent_val'+goodsNutrition.i+'" value='+o.goods_basic[i]['npct']+' style="width:150px;margin:auto;" maxlength="20">';
            basicStr += '</td>';

            if (o.goods_basic[i]['useFlg'] == 1) {
                basicStr += '<td style="text-align:center;vertical-align:middle;">';
                basicStr += '<input type="radio" class="square-radio"  name="basic_useFlg'+ goodsNutrition.i+'" value="1" checked>启用' +
                        '&nbsp;<input type="radio" class="square-radio"  name="basic_useFlg'+goodsNutrition.i+'" value="0">禁用';
                basicStr += '</td>';
            } else {
                basicStr += '<td style="text-align:center;vertical-align:middle;">';
                basicStr += '<input type="radio" class="square-radio"  name="basic_useFlg'+goodsNutrition.i+'" value="1" >启用' +
                        '&nbsp;<input type="radio" class="square-radio"  name="basic_useFlg'+goodsNutrition.i+'" value="0" checked>禁用';
                basicStr += '</td>';
            }

            basicStr += '<td style="text-align:center;vertical-align:middle;" >';
            basicStr += '<img src="/quanliadmin/docroot/images/backend/icon_asc.gif" title="向上移动" style="cursor:pointer;" onclick="goodsNutrition.upMove(this);">';
            basicStr += '<img src="/quanliadmin/docroot/images/backend/icon_desc.gif" title="向下移动" style="cursor:pointer;margin-left:12px;" onclick="goodsNutrition.downMove(this);">';
            basicStr += '<img src="/quanliadmin/docroot/images/backend/icon_delete.gif" title="删除" value='+o.goods_basic[i]['specID']+' style="cursor:pointer;margin-left:12px;" onclick="goodsNutrition.delMove(this);">';
            basicStr += '</td>';

            basicStr += '</tr>';
        }
        $('.specBody').append(basicStr);

        $('.square-radio').iCheck({
            checkboxClass: 'icheckbox_square-blue',
            radioClass: 'iradio_square-blue',
            increaseArea: '20%' // optional
        });
    },


    check: function() {
        this.dt = E.getFormValues("editForm");
        var error_msg = "";

        if ($.trim(this.dt.nGroupName) == "") {
            error_msg += "营养成分信息组名称不能为空<br />";
        }

        if (!E.isInt(this.dt.sortOrder)) {
            error_msg += "排序序号必须为数字<br />";
        }

        //使用标志
        var use_array = [];
        //营养成分值
        var basic_array = [];
        //营养成分百分比
        var percent_array = [];

        for(var i=1;i<=goodsNutrition.i;i++) {

            if ($('[name=basic_useFlg'+i+']:checked').val()) {
                use_array.push($('[name=basic_useFlg'+i+']:checked').val());
            }

            if ($('[name=basic_val'+i+']').val()) {
                basic_array.push($('[name=basic_val'+i+']').val());
            }

            if ($('[name=percent_val'+i+']').val()){
                percent_array.push($('[name=percent_val'+i+']').val());
            }
        }

        if (basic_array.length == 0) {
            error_msg += "请添加营养成分值<br />";
        }

        //添加基本信息不能为空
        $('[name*=basic_val]').each(function(){
            var v = E.trim($(this).val());

            if (v == '') {
                error_msg += '有未添加的基本信息<br />';
                return false;
            }
        });

//        if (percent_array.length == 0) {
//            error_msg += "请添加营养成分百分比<br />";
//        }

        this.basic_array = basic_array;
        this.percent_array = percent_array;
        this.use_array = use_array;

        if (error_msg != "") {
            E.alert(error_msg);
        }else {
            goodsNutrition.addSave() ;
        }
    },

    //保存规格信息
    addSave: function() {

        E.loadding.open("请稍候");
        $nGroupID = $("#nGroupID").val();

        $.ajax({
            type: "POST",
            url: "/invokingAjax-backend/goods/goodsNutrition.ajax?operFlg=4" ,
            dataType: "JSON",
            data: {
                nGroupID       : this.dt.nGroupID,
                nGroupName     : this.dt.nGroupName,
                sortOrder      : this.dt.sortOrder,
                useFlg         : this.dt.useFlg,
                basic_array   : this.basic_array,
                percent_array : this.percent_array,
                use_array      : this.use_array
            },
            success: function( result_obj ) {

                E.loadding.close();

                if (result_obj.code == 200) {

                    goodsNutrition.load();
                    goodsNutrition.i=0;
                    if(result_obj.nGroupID){
                        E.alert(result_obj.message, 2,"E.popup.close");
                    }else{
                        $("#nGroupName").val('');
                        $("#sortOrder").val(result_obj.sortOrder);
                        $(".specBody").html('');
                        E.alert(result_obj.message, 2);
                    }
                } else {
                    E.alert(result_obj.message);
                }
            }
        });
    },

    addload : function() {
        window.location.href = 'goodsNutrition.pscript?operFlg=1';
    },

    //最大序列号重置
    sortUp:function(o){
        E.loadding.close();
        $("#sortOrder").val(o.sortOrder);
    },

    load: function() {
        $("#flex").flexReload();
    },

    //添加商品营养成分信息
    specAdd : function(){
        this.i++;
        var specStr = '';

        specStr += '<tr>';

        specStr += '<td style="text-align:center;">';
        specStr += '<input  class="form-control w300" type="text" name="basic_val'+goodsNutrition.i+'" style="width:150px;margin:auto;" maxlength="20">';
        specStr += '</td>';

        specStr += '<td style="text-align:center;">';
        specStr += '<input  class="form-control w300" type="text" name="percent_val'+goodsNutrition.i+'" style="width:150px;margin:auto;" maxlength="20">';
        specStr += '</td>';

        specStr += '<td style="text-align:center;vertical-align:middle;">';
        specStr += '<input type="radio" class="square-radio" class="basicRadio" name="basic_useFlg'+goodsNutrition.i+'" value="1" checked>启用' +
                '&nbsp;<input type="radio" class="square-radio" class="basicRadio" name="basic_useFlg'+goodsNutrition.i+'" value="0">禁用';
        specStr += '</td>';

        specStr += '<td style="text-align:center;vertical-align:middle;" >';
        specStr += '<img src="/quanliadmin/docroot/images/backend/icon_asc.gif" title="向上移动" style="cursor:pointer;" onclick="goodsNutrition.upMove(this);">';
        specStr += '<img src="/quanliadmin/docroot/images/backend/icon_desc.gif" title="向下移动" style="cursor:pointer;margin-left:12px;" onclick="goodsNutrition.downMove(this);">';
        specStr += '<img src="/quanliadmin/docroot/images/backend/icon_delete.gif" title="删除" style="cursor:pointer;margin-left:12px;" onclick="goodsNutrition.delMove(this);">';
        specStr += '</td>';
        specStr += '</tr>';

        $('.specBody').append(specStr);

        $('.square-radio').iCheck({
            checkboxClass: 'icheckbox_square-blue',
            radioClass: 'iradio_square-blue',
            increaseArea: '20%' // optional
        });
    },

    upMove : function(obj){
        var objParentTR = $(obj).parent().parent();
        var prevTR = objParentTR.prev();
        if (prevTR.length > 0) {
            prevTR.insertAfter(objParentTR);
        }
    },

    downMove : function(obj){
        var objParentTR = $(obj).parent().parent();
        var nextTR = objParentTR.next();
        if (nextTR.length > 0) {
            nextTR.insertBefore(objParentTR);
        }
    },

    delMove : function(obj){

        //检查当前商品组信息
        var currentSpec = $(obj).attr('value');
        $(obj).parent().parent().remove();
    },

    clearUp: function(){
        $('#nGroupName1').val('');
        $('#useFlg').val('');
    },

    //删除
    del: {

        p_array: '',

        check: function() {

            var p_obj = $('#flex').find('tr.trSelected');
            if (p_obj.length == 0) {
                E.alert('请选择要删除的商品基本信息组');
            } else {
                var temp_array = new Array();
                $.each(p_obj, function(k, v) {
                    var id = $(v).attr('id').replace('row', '');
                    temp_array.push($(v).attr('id').replace('row', ''));
                });

                this.p_array = temp_array;
                E.confirm('您确认要删除选中的商品基本信息组吗？', 'goodsNutrition.del.execute');
            }

        },

        execute: function() {
            E.loadding.open('正在删除商品基本信息组，请稍候...');
            E.ajax_post({
                url: '/invokingAjax-backend/goods/goodsNutrition.ajax?operFlg=5',
                data: {
                    p_array: this.p_array
                },
                call: 'goodsNutrition.del.result'
            });
        },

        result: function( o ) {
            E.loadding.close();
            if (o.code == 200) {
                E.alert('删除成功', 2, 'goodsNutrition.load');
            } else {
                E.alert(o.message);
            }
        }

    }

};

</script>
</body>
</html>